//
//  GEEBaseViewController.m
//  KX11UI
//
//  Created by LJQ on 2021/2/19.
//

#import "BaseViewController_HYC.h"
//#import "GEE_pushModel.h"
//#import "NSDate+HSSYDate.h"

static NSString *barKey1 = @"firstBarKey";
static NSString *barKey2 = @"secondBarkey";
static NSString *barKey3 = @"barKey3";



@interface BaseViewController_HYC ()<UIGestureRecognizerDelegate,UITableViewDelegate , UITableViewDataSource>
@property (nonatomic ,strong) UIView* stateView;
@property (nonatomic,strong)UITableView *tableViewH;

@end

@implementation BaseViewController_HYC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [UIViewController attemptRotationToDeviceOrientation];
    //  _________________________________________________________________________________________________HYC_________________________________________________________________________________
    //start
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    
    self.stateView = [[UIView alloc] initWithFrame:CGRectMake(0, -SCREEN_H_NAV_HEIGHT, SCREEN_H_WIDTH, SCREEN_H_NAV_HEIGHT)];
    
    _stateView.backgroundColor = UIColor.whiteColor;
    
    if (!_stateView) {
        [self.navigationController.navigationBar addSubview:_stateView];
    }
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    
    
    
    //  _________________________________________________________________________________________________HYC_________________________________________________________________________________
    //Over
    
    self.view.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
//    if (@available(iOS 11.0 , *)) {
//        UITableView.appearance.estimatedRowHeight = 0;
//        UITableView.appearance.estimatedSectionFooterHeight = 0;
//        UITableView.appearance.estimatedSectionHeaderHeight = 0;
//        [[UITabBar appearance] setTranslucent:NO];
//    }else {
//        self.automaticallyAdjustsScrollViewInsets = false;
//    }
//    [[UITabBar appearance] setTranslucent:NO];
    self.extendedLayoutIncludesOpaqueBars = true;
    self.edgesForExtendedLayout = UIRectEdgeNone;

    
    
    
    
    //返回按钮
    self.H_leftBackImage = UIImageView.alloc.init;
    self.H_leftBackImage.image = @"ZLVH_nav_back_HYC".H_image;
    /**
      start modify by 胡云超 2022-07-04 for DC1E - 给返回按钮埋点
     **/
    
//    NSMutableDictionary *backDict = [NSMutableDictionary dictionaryWithDictionary:@{@"$element_content": @"返回"}];
//    if (self.trackTitle.length) backDict[@"$title"] = self.trackTitle;
//    self.leftBackImage.sensorsAnalyticsViewProperties = backDict;
    /**
      end - DC1E - 给返回按钮埋点
     **/
    WeakSelf_HYC
    [self.H_leftBackImage addGuest:tapAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
            
        [weakSelf H_leftBtnClick];
    }];
    [self H_setLeftView:self.H_leftBackImage];
    
}


// 禁用返回手势 _________________________________________________________________________________________________HYC_________________________________________________________________________________
//
- (void)H_popGestureClose
{
    // 禁用侧滑返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势禁用
        for (UIGestureRecognizer *popGesture in self.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = NO;
        }
    }
}

- (void)H_popGestureOpen
{
    // 启用侧滑返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势启用
        for (UIGestureRecognizer *popGesture in self.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = YES;
        }
    }
}
//  _________________________________________________________________________________________________HYC_________________________________________________________________________________
//









- (void)H_leftBtnClick {
    
    [self H_pop];

    
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(void)H_popTo:(UIViewController *)vc {
    for (UIViewController *item in self.navigationController.viewControllers) {
        if ([item isKindOfClass:vc.class]) {
            [self.navigationController popToViewController:item animated:true];
        }else {
            NSLog(@"未找到页面");
        }
    }
}

/// 返回
-(void)H_pop {
    [self.navigationController popViewControllerAnimated:true];
}

/// 跳转
/// @param vc vc description
-(void)H_push:(UIViewController *)vc {
    [self.navigationController pushViewController:vc animated:true];
}

/// 跳转是否隐藏Tabbar
/// @param vc vc description
/// @param hidden hidden description
-(void)H_push:(UIViewController *)vc isBottomHidden:(BOOL)hidden {
//    [self.navigationController setHidesBottomBarWhenPushed:hidden];
    [self setHidesBottomBarWhenPushed:hidden];
    [self H_push:vc];
    [self setHidesBottomBarWhenPushed:!hidden];
}


/// rightBarBtns
/// @param first first description
/// @param firstBlock firstBlock description
/// @param second second description
/// @param secondBlock secondBlock description
-(NSArray<UIBarButtonItem *> *)H_setRightItems:(NSString *)first firstBlock:(BarBtnBlock)firstBlock second:(NSString *)second second:(BarBtnBlock)secondBlock {
    UIImage *img1 = first.H_image;
    UIImage *img2 = first.H_image;
    img1 = [img1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    img2 = [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] init];
    UIBarButtonItem *barBtn2 = [[UIBarButtonItem alloc] init];
    
    objc_setAssociatedObject(self, &barKey1, firstBlock, OBJC_ASSOCIATION_COPY);
    objc_setAssociatedObject(self, &barKey2, secondBlock, OBJC_ASSOCIATION_COPY);
    
    if (img1) {
        [barBtn1 setImage:img1];
    }else {
        [barBtn1 setTitle:first];
    }
    [barBtn1 setTarget:self];
    [barBtn1 setAction:@selector(barBtnCallBack1:)];
    
    if (img2) {
        [barBtn2 setImage:img2];
    }else {
        [barBtn2 setTitle:second];
    }
    [barBtn2 setTarget:self];
    [barBtn2 setAction:@selector(barBtnCallBack2:)];
    
    self.navigationItem.rightBarButtonItems = @[barBtn1,barBtn2];
    return self.navigationItem.rightBarButtonItems;
}

-(void)barBtnCallBack1:(BarBtnBlock)block {
    BarBtnBlock guestBlock1 = objc_getAssociatedObject(self, &barKey1);
    if (guestBlock1) {
        guestBlock1();
    }
}

-(void)barBtnCallBack2:(BarBtnBlock)block {
    BarBtnBlock guestBlock2 = objc_getAssociatedObject(self, &barKey2);
    if (guestBlock2) {
        guestBlock2();
    }
}



/// navigationBar rightItem
/// @param itemView 按钮
/// @param idBlock idBlock description
-(void)H_setRightView:(UIView *)itemView idBlock:(BarBtnBlock)idBlock {
    [itemView setUserInteractionEnabled:YES];
    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] initWithCustomView:itemView];
    objc_setAssociatedObject(self, &barKey3, idBlock, OBJC_ASSOCIATION_COPY);
    [itemView addGuest:tapAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
        idBlock();
    }];
    
    self.navigationItem.rightBarButtonItems = @[barBtn1];
    
}

-(NSArray<UIBarButtonItem *> *)H_setRightViews:(NSArray <UIView *>*)views touchBlock:(nullable void(^)(UIView *buttonItem ,NSInteger index))block {
    
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    
    
        for (NSInteger i = 0; i < views.count; i ++ ) {

        UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] init];
        [barBtn setCustomView:views[i]];
        [arr addObject:barBtn];
        
        [views[i] addGuest:tapAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
            
            block ? block(gesture.view,i):block;
            
        }];
    }
    self.navigationItem.rightBarButtonItems = arr;
    return self.navigationItem.rightBarButtonItems;
}

/// navigationBar rightItem
/// @param itemView 点击label
/// @param editStr 编辑文字
/// @param finishStr 完成文字
/// @param idBlock idBlock description
-(void)H_setRightView:(UILabel *)itemView editStr:(NSString *)editStr finishStr:(NSString *)finishStr  idBlock:(void(^)(bool select))idBlock {
    [itemView setUserInteractionEnabled:YES];
    itemView.text = editStr;
    __block BOOL isSelect = NO;
    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] initWithCustomView:itemView];
    objc_setAssociatedObject(self, &barKey3, idBlock, OBJC_ASSOCIATION_COPY);
    [itemView addGuest:tapAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
        if (isSelect) {
            itemView.text = editStr;
            isSelect = NO;
        }else {
            itemView.text = finishStr;
            isSelect = YES;
        }
        idBlock(isSelect);
    }];
    
    self.navigationItem.rightBarButtonItems = @[barBtn1];
    
}




/// navigationBar leftItem
/// @param itemView 左侧标题
-(void)H_setLeftView:(UIView *)itemView {
    [itemView setUserInteractionEnabled:YES];
    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] initWithCustomView:itemView];
    self.navigationItem.leftBarButtonItems = @[barBtn1];
}





@end
